package com.sxhuayuan.parking.controller.admin;

import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.Salesman;
import com.sxhuayuan.parking.service.AdminService;
import com.sxhuayuan.parking.service.SalesmanService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author helong
 * @create 2019-08-02 16:25
 */
@RestController("adminController")
@RequestMapping("/admin/salesman")
public class SalesmanController extends BaseController {

	@Resource(name = "salesmanServiceImpl")
	SalesmanService service;

	@Resource(name = "adminServiceImpl")
	AdminService adminService;

	@GetMapping("/all")
	public RespMessage getAll() {
		return successResp(service.findAll());
	}

	@PostMapping("/list")
	public RespMessage pageList(@RequestBody Pageable pageable) {
		return successResp(service.findPage(pageable));
	}

	/**
	 * 锁定 启用
	 */
	@RequestMapping("/locked/or/enabled")
	public RespMessage enable(Long id, String type, Boolean value) {
		if (!adminService.isAuthenticated()) {
			return invalidToken();
		}
		if (Objects.isNull(id) || Objects.isNull(value) || Objects.isNull(type)) {
			return RespMessage.error(RespMessage.Status.VALID_ERR, Message.FORM_VALID_FAIL);
		}
		Salesman salesman = service.findOne(id);
		if ("Locked".equals(type)) {
			salesman.setIsLocked(value);
			service.update(salesman);
			return RespMessage.success();
		} else if ("Enabled".equals(type)) {
			salesman.setIsEnabled(value);
			service.update(salesman);
			return RespMessage.success();
		} else {
			return RespMessage.error("type错误");
		}
	}

	/** 更新 */
	@PostMapping("/update")
	public RespMessage update(@RequestBody Salesman salesman) {
		if (!adminService.isAuthenticated()) {
			return invalidToken();
		}
		if (salesman == null || salesman.getId() == null) {
			return RespMessage.error("ID不能为空");
		}
		Salesman one = service.findOne(salesman.getId());
		if (one == null) {
			return RespMessage.error("该业务员不存在");
		}
		if (StringUtils.isEmpty(salesman.getUsername()) || StringUtils.isEmpty(salesman.getName())) {
			return RespMessage.error("用户名和姓名不能为空");
		}
		if (salesman.getUsername().length() < 2 || salesman.getName().length() < 2) {
			return RespMessage.error("用户名和姓名的长度最少为两位");
		}
		Salesman salesman1 = service.findByUsername(salesman.getUsername());
		if (salesman1 != null && !salesman1.getId().equals(salesman.getId())) {
			return RespMessage.error("用户名已存在");
		}
		if (StringUtils.isNotEmpty(salesman.getPassword())) {
			one.setPassword(DigestUtils.md5Hex(salesman.getPassword()));
		}
		one.setUsername(salesman.getUsername());
		one.setName(salesman.getName());
		service.update(one);
		return RespMessage.success();
	}

	@PostMapping("/save")
	public RespMessage save(@RequestBody Salesman salesman) {
		if (!adminService.isAuthenticated()) {
			return invalidToken();
		}
		if (StringUtils.isEmpty(salesman.getUsername()) || StringUtils.isEmpty(salesman.getName())) {
			return RespMessage.error("用户名和姓名不能为空");
		}
		if (salesman.getUsername().length() < 2 || salesman.getName().length() < 2) {
			return RespMessage.error("用户名和姓名的长度最少为两位");
		}
		if (service.usernameExists(salesman.getUsername())) {
			return RespMessage.error("用户名已存在");
		}
		if (StringUtils.isEmpty(salesman.getPassword())) {
			return RespMessage.error("密码不能为空");
		}
		salesman.setId(null);
		salesman.setPassword(DigestUtils.md5Hex(salesman.getPassword()));
		service.save(salesman);
		return RespMessage.success();
	}

	/** 根据id删除用户 */
	@PostMapping("/del/{id}")
	public RespMessage del(@PathVariable Long id) {
		if (!adminService.isAuthenticated()) {
			return invalidToken();
		}
		Salesman one = service.findOne(id);
		if (one == null) {
			return RespMessage.error("用户不存在");
		}
		service.delete(one);
		return RespMessage.success();
	}
}
